<!DOCTYPE html>
<html lang="zh_CN" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8"/>
    <title>内容管理列表 Content List</title>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, content-scalable=0"/>
    <link rel="stylesheet" th:href="@{/statics/layui/css/layui.css}" media="all"/>
    <link rel="stylesheet" th:href="@{/statics/style/admin.css}" media="all"/>

    <style type="text/css">
        .layui-btn-dropdown{position:relative;display:inline-block}
        .layui-btn-dropdown .layui-dropdown-menu{position:absolute;top:100%;left:0;z-index:1000;float:left;display:none;margin:2px 0 0;padding:5px 0;min-width:120px;border:1px solid #ccc;border:1px solid rgba(0,0,0,.15);border-radius:4px;background-color:#fff;background-clip:padding-box;-webkit-box-shadow:0 6px 12px rgba(0,0,0,.175);box-shadow:0 6px 12px rgba(0,0,0,.175);list-style:none;text-align:left;font-size:.875rem;-webkit-background-clip:padding-box}
        .layui-btn-dropdown .layui-dropdown-menu>li>a{clear:both;display:block;padding:3px 15px;color:#333;white-space:nowrap;font-weight:400;line-height:1.42857143}
        .layui-btn-dropdown .layui-dropdown-menu>li>a:focus,.layui-btn-dropdown .layui-dropdown-menu>li>a:hover{background-color:#f0f0f0;text-decoration:none}
        .layui-btn-dropdown .layui-dropdown-menu .divider{overflow:hidden;margin:9px 0;height:1px;background-color:#e5e5e5}
    </style>

    <script th:inline="javascript">
        var moduleCode = [[${module.code}]];
        var moduleName = [[${module.name}]];
        var navId = [[${nav}]];
        var typeInt = [[${type}]];
    </script>
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-form layui-card">
                <div class="layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <select  name="layui-form-field" id="layui-form-field" class="form-control input-sm" style="width:auto;">
                                <option value="" >选择搜索条件</option>
                                <option value="navName" >栏目名称</option>
                                <option value="title"  >文章标题</option>
                                <option value="content" >文章内容</option>
                            </select>
                        </div>
                        <div class="layui-inline">
                            <input class="layui-input" name="keyword" id="layui-form-keyword" autocomplete="off"/>
                        </div>
                        <button class="layui-btn" data-type="search">搜索</button>
                    </div>
                </div>
                <div class="layui-card-body">
                    <table class="layui-hide" id="content-list-table" lay-filter="content-list-table"></table>

                    <script type="text/html" id="content-list-table-operate-bar">
                        <a th:if="${editAction}" class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>修改</a>
                        <a th:if="${removeAction}" class="layui-btn layui-btn-primary layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                    </script>

                    <script type="text/html" id="content-list-table-toolbar">
                            <div th:if="${addAction && #lists.size(navs) > 0}" class="layui-btn-dropdown">
                                <button class="layui-btn layui-btn-sm"  data-toggle="dropdown" lay-event="toggle">
                                    添加[[${module.name}]]到栏目  <span class="layui-icon" style="font-size: 14px"></span>
                                </button>
                                <ul class="layui-dropdown-menu">
                                    <li><a lay-event="add" th:each="nv : ${navs}" th:data-nav="${nv.id}" th:text="${nv.name}">NAV</a></li>
                                    <!--li class="divider"></li--><!--分割线-->
                                </ul>
                            </div>
                            <button th:if="${removeAction}" class="layui-btn layui-btn-sm layui-btn-primary" lay-event="batch-remove">批量删除</button>
                    </script>
                    <blockquote class="layui-elem-quote">
                        <div th:if="${#lists.size(navs) > 1}" style="margin-bottom: 10px;">
                            <div class="layui-btn-group content-table-operate-btn" >
                                <button class="layui-btn" data-type="switchNav">转到</button>
                            </div>
                            <div class="layui-inline">
                                <select is="navmenu" name="navmenu">
                                    <option>[[${module.name}]]相关的其它导航栏目</option>
                                    <option th:each="nv : ${navs}" th:value="${nv.id}" th:text="${nv.name}"></option>
                                </select>
                            </div>
                        </div>
                        <div>
                            请先在网站导航中添加模型 <span style="color: #1E9FFF"><b>[[${module.name}]]</b></span> 的导航菜单，然后才能在此 <span style="color: #009688"><b>添加[[${module.name}]]</b></span> 或 <span style="color: #009688"><b>转到栏目</b></span> 到相关的导航菜单的操作。
                        </div>
                   </blockquote>
                </div>
            </div>
        </div>
    </div>
</div>

<script th:src="@{/statics/layui/layui.js}"></script>
<script th:inline="none">
    layui.config({
        base: '/statics/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table'], function () {
        var $ = layui.$
            , admin = layui.admin
            , form = layui.form
            , table = layui.table;

        var fieldName = $('#layui-form-field');
        var keyword = $('#layui-form-keyword');

        var tableIns = table.render({
            elem: '#content-list-table'
            , url: '/api/content/page?navId='+ navId + '&module=' + moduleCode + '&type=' + typeInt
            , id: 'id'
            , toolbar: '#content-list-table-toolbar'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'id', width: 60, title: 'ID'}
                , {field: 'navName', width: 120, title: '导航栏目', sort: true}
                , {field: 'title', title: '标题',templet: function(d){
                    if( d.picture == null || d.picture.trim.length == 0){
                        return d.title;
                    }else {
                        return '<span class="layui-badge layui-bg-orange">图</span>' + d.title;
                    }
                }}
                , {field: 'publishTime', width: 120, title: '发布时间', sort: true,templet: function(d){
                    return d.publishTime.substring(0,10);
                }}
                , {
                    field: 'state', width: 80, title: '状态', fixed: 'right', sort: true, templet: function (d) {
                        if (d.state == '1') {
                            return '<button class="layui-btn layui-btn-xs">正常</button>';
                        } else {
                            return '<button class="layui-btn layui-btn-primary layui-btn-xs">隐藏</button>';
                        }
                    }
                }
                , {field: 'visits', width: 80, title: '访问量', fixed: 'right'}
                , {width: 150, align: 'center', fixed: 'right', toolbar: '#content-list-table-operate-bar'}
            ]]
            , page: true
            , request: {
                pageName: 'pageNum'
                , limitName: 'pageSize'
                , nav: navId
                , module: moduleCode
            }
        });

        //头工具栏事件
        table.on('toolbar(content-list-table)', function (obj) {
            var checkStatus = table.checkStatus('id')
                ,data = checkStatus.data;
            switch (obj.event) {
                case 'toggle':
                    $('.layui-btn-dropdown').find('ul').toggle();
                    break;
                case 'batch-remove':
                    var ids = '';
                    $.each(data, function(idx, obj) {
                        ids += obj.id + ',';
                    });
                    console.log(ids);
                    ids = ids.substr(0,ids.length-1);
                    if( ids.length == 0 ) {
                        layer.msg('请选择要删除的记录!');
                    }else {
                        $.ajax({
                            url: '/api/content/batchRemove?ids=' + ids,
                            method: 'DELETE',
                            data: data.field,
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    tableIns.reload({page: {curr: 1}}); //数据刷新
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    }
                    break;
                case 'add':
                    $('.layui-btn-dropdown').find('ul').hide();
                    var nav = $(this).data('nav');
                    var index = layer.open({
                        type: 2
                        , title: '添加'+moduleName
                        , content: '/web/content/add?navId='+nav+'&module='+moduleCode+'&type='+typeInt
                        , maxmin: false
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-content-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/add',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            layer.msg(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
            }
            ;
        });

        //监听行工具事件
        table.on('tool(content-list-table)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    layer.confirm('真的删除么', function (index) {
                        $.ajax({
                            url: '/api/content/remove?id=' + data.id,
                            method: 'delete',
                            contentType: "application/json;charset=UTF-8",
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    layer.msg('记录已删除!', {icon: 1, time: 1000}, function () {
                                        obj.del();
                                        layer.close(index); //关闭弹层
                                    });
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    });
                    break;
                case 'edit':
                    var index = layer.open({
                        type: 2
                        , title: '修改'+moduleName
                        , content: '/web/content/edit?id=' + data.id
                        , maxmin: false
                        , area: ['500px', '450px']
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-content-add-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);

                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: '/api/content/edit',
                                    method: 'post',
                                    data: data.field,
                                    dataType: 'JSON',
                                    success: function (res) {
                                        if (res.code == '0') {
                                            tableIns.reload({page: {curr: 1}}); //数据刷新
                                            layer.close(index); //关闭弹层
                                        } else {
                                            alert(res.msg);
                                        }
                                    },
                                    error: function (err) {
                                    }
                                });

                            });

                            submit.trigger('click');
                        }
                    });
                    layer.full(index);
                    break;
            }
        });

        var active = {
            search: function () {
                //执行重载
                tableIns.reload({
                    where: {
                        fieldName : fieldName.val(),
                        keyword : keyword.val()
                    }
                });
            }
        };
        $('.layui-form .layui-form-item .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        var activedo = {
            switchNav: function(){ //获取选中数据
                var navId = $("#navmenu").val();
                var checkStatus = table.checkStatus('id')
                    ,data = checkStatus.data;
                var ids = '';
                $.each(data, function(idx, obj) {
                    ids += obj.id + ',';
                });
                console.log( 'ids=' + ids);
                console.log( 'navId=' + navId);
                ids = ids.substr(0,ids.length-1);
                if( ids.length == 0 ) {
                    layer.msg('请选择记录!');
                }else {
                    if( navId.length == 0 ){
                        layer.msg('请选择导航栏目!');
                    }else {
                        $.ajax({
                            url: '/api/content/switchNav?ids=' + ids + '&navId=' + navId,
                            method: 'DELETE',
                            data: data.field,
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == '0') {
                                    tableIns.reload({page: {curr: 1}}); //数据刷新
                                } else {
                                    layer.msg(res.msg);
                                }
                            },
                            error: function (err) {
                            }
                        });
                    }
                }
            }
        };

        $('.content-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            activedo[type] ? activedo[type].call(this) : '';
        });

    });
</script>
</body>
</html>